//
//  HHMessagesDemoViewController.m
//  HHChatKit
//
//  Created by Henry on 2021/5/10.
//

#import "HHMessagesDemoViewController.h"
#import <HHTool/HHTool.h>
#import "HHChatDetailDemoViewController.h"
#import "HHResources.h"
#import "HHNoticeCallContentView.h"

typedef NS_ENUM(NSInteger, HHKeyboardMoreItemType) {
    HHKeyboardMoreItemTypeNone  = 0,
    HHKeyboardMoreItemTypePhoto = 1,
    HHKeyboardMoreItemTypeCamera = 2,
    HHKeyboardMoreItemTypeAudio = 3,
    HHKeyboardMoreItemTypeOnlineMeeting = 4,
    HHKeyboardMoreItemTypeOnlineMeetingTip = 5,
    HHKeyboardMoreItemTypeNetworkCall = 6,
    HHKeyboardMoreItemTypeDoubleLiveMeeting = 7,
    HHKeyboardMoreItemTypeUseful = 8,
    HHKeyboardMoreItemTypeVirtualDesktop = 9,
    HHKeyboardMoreItemTypeImageSynchronization = 10,
    HHKeyboardMoreItemTypeOnlineTrain = 11,
    HHKeyboardMoreItemTypeOnlineLive = 12,
    HHKeyboardMoreItemTypeInterfaceService = 13,
    HHKeyboardMoreItemTypeCall             = 14,  // 通话
    HHKeyboardMoreItemTypeCase             = 15,  // 病历分享
};


@interface HHMessagesDemoViewController () <HHMessagesViewControllerDelegate, HHNoticeCallContentDelegate>

@property (nonatomic, strong) HHNoticeCallContentView *noticeCallContentView;

@end

@implementation HHMessagesDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [self setupMoreItem];
    [self setupRightItem];
}

- (void)setupRightItem {
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(onClickRight)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setup_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickRight)];
}

- (void)onClickRight {
    HHChatDetailDemoViewController *vc = [[HHChatDetailDemoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setupMoreItem {
    UIImage *image = [HHResources getImageFromMessage:@"chat_more_icons_camera"];
    HHKeyboardMoreItem *item = [HHKeyboardMoreItem moreItemWithType:HHKeyboardMoreItemTypePhoto title:@"相册" image:image];
    HHKeyboardMoreItem *item1 = [HHKeyboardMoreItem moreItemWithType:HHKeyboardMoreItemTypeCamera title:@"拍照" image:image];

    NSMutableArray *arary = @[item, item1, item, item, item1, item, item, item, item, item, item].mutableCopy;
    
    [self setMoreItem:arary];
}

- (void)loadMessage:(HHMessageCellData *)msg complation:(void (^)(BOOL, NSArray * _Nonnull))complation {
    
    HHTextMessageCellData *data = [[HHTextMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data.content = @"1";
    data.time = @"1622613066274";
    data.name = @"sender1";
    data.showTime = YES;
    data.showName = YES;
    data.sender = @"4";
    data.conversationId = self.conversationId;
    
    HHTextMessageCellData *data1 = [[HHTextMessageCellData alloc] initWithDirection:HHMsgDirectionOutgoing];
    data1.conversationId = self.conversationId;
    data1.content = @"1";
    data1.time = @"1622613104";
    data1.name = @"sender2";
    data1.showTime = YES;
    data1.showName = YES;
    data1.sender = @"4";
    data1.status = HHMsgStatusFail;
//    self.dataArray = @[data, data1].mutableCopy;

    HHTextMessageCellData *data2 = [[HHTextMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data2.conversationId = self.conversationId;
    data2.content = @"2";
    data2.time = @"1622613066274";
    data2.name = @"sender1";
    data2.sender = @"4";
//    data2.showTime = YES;
//    data2.showName = YES;

    HHTextMessageCellData *data3 = [[HHTextMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data3.conversationId = self.conversationId;
    data3.content = @"2222";
    data3.time = @"1622613066274";
    data3.name = @"sender1";
//    data2.showTime = YES;
//    data2.showName = YES;

    HHTextMessageCellData *data11 = [[HHTextMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data11.conversationId = self.conversationId;
    data11.content = @"私服圣诞节覅集散地附近时代峰is大家覅的送风机时代峻峰斯蒂芬";
    data11.time = @"1622613104353";
    data11.name = @"sender2";
    data11.showTime = YES;
    data11.showName = YES;
    data11.status = HHMsgStatusSending;
    
    HHSystemMessageCellData *data9 = [[HHSystemMessageCellData alloc] init];
    data9.conversationId = self.conversationId;
    data9.content = @"系统消息1";
    data9.time = @"1622613104353";
    data9.name = @"sender2";
    data9.showTime = YES;
    
    HHSystemMessageCellData *data91 = [[HHSystemMessageCellData alloc] init];
    data91.conversationId = self.conversationId;
    data91.content = @"系统消息2";
    data91.time = @"1622613104353";
    data91.name = @"sender2";
    data91.showTime = NO;
    
    HHTextMessageCellData *data12 = [[HHTextMessageCellData alloc] initWithDirection:HHMsgDirectionOutgoing];
    data12.conversationId = self.conversationId;
    data12.content = @"系统消息2";
    data12.time = @"1622613104353";
    data12.name = @"sender2";
    data12.showTime = YES;
    data12.showName = YES;
    data12.status = HHMsgStatusSending;
    
    HHImageMessageCellData *data31 = [[HHImageMessageCellData alloc] initWithDirection:HHMsgDirectionOutgoing];
    data31.conversationId = self.conversationId;
    data31.uuid = @"31";
    data31.time = @"1622613104353";
    data31.name = @"sender2";
    data31.status = HHMsgStatusSending;
    HHImageItem *item1 = [[HHImageItem alloc] init];
    item1.url = @"https://img-pre.ivsky.com/img/tupian/pre/202011/28/feizhouju-001.jpg";
    item1.size = CGSizeMake(800, 600);
    data31.items = @[item1].mutableCopy;
    
    HHImageMessageCellData *data32 = [[HHImageMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data32.conversationId = self.conversationId;
    data32.uuid = @"32";
    data32.time = @"1622613104353";
    data32.name = @"sender2";
    HHImageItem *item2 = [[HHImageItem alloc] init];
    item2.url = @"//https://img-pre.ivsky.com/img/tupian/pre/202011/28/feizhouju-001.jpg";
    item2.size = CGSizeMake(800, 600);
    data32.items = @[item2].mutableCopy;
    
    HHVoiceMessageCellData *data41 = [[HHVoiceMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data41.conversationId = self.conversationId;
    data41.uuid = @"41";
    data41.time = @"1622613104353";
    data41.name = @"sender2";
    data41.showTime = YES;
    data41.duration = 10;
    
    HHVoiceMessageCellData *data42 = [[HHVoiceMessageCellData alloc] initWithDirection:HHMsgDirectionOutgoing];
    data42.conversationId = self.conversationId;
    data42.uuid = @"42";
    data42.time = @"1622613104353";
    data42.duration = 1;
    data42.type = @"mp3";
    data42.url = @"https://www.yytzklm.com//voice/272sjfo400101.mp3";
    
    
    HHVideoMessageCellData *data51 = [[HHVideoMessageCellData alloc] initWithDirection:HHMsgDirectionOutgoing];
    data51.conversationId = self.conversationId;
    data51.uuid = @"51";
    data51.time = @"1622613104353";
    data51.name = @"sender2";
    HHVideoItem *videoItem51 = [[HHVideoItem alloc] init];
    videoItem51.url = @"https://vd2.bdstatic.com/mda-mf62wwzatmk99a0t/540p/h264_cae/1623031393955695634/mda-mf62wwzatmk99a0t.mp4?v_from_s=gz_haokan_4469&auth_key=1623206029-0-0-cd2485d635de055fee853e0952b81271&bcevod_channel=searchbox_feed&pd=1&pt=3&abtest=";
    videoItem51.duration = 15;
    HHSnapshotItem *snapshotItem51 = [[HHSnapshotItem alloc] init];
    snapshotItem51.url = @"https://tukuimg.bdstatic.com/processed/08e5c9b6660d8f935b19406ef4a1c08d.jpeg@s_2,w_681,h_381,q_100,f_webp";
    snapshotItem51.size = CGSizeMake(681, 381);
    data51.snapshotItem = snapshotItem51;
    data51.videoItem = videoItem51;
    
    HHFaceMessageCellData *data61 = [[HHFaceMessageCellData alloc] initWithDirection:HHMsgDirectionOutgoing];
    data61.time = @"1622613104353";
    data61.name = @"sender2";
    data61.sender = @"1";
//    data42.showTime = YES;
    
    HHFileMessageCellData *data71 = [[HHFileMessageCellData alloc] initWithDirection:HHMsgDirectionOutgoing];
    data71.conversationId = self.conversationId;
    data71.time = @"1622613104353";
    data71.name = @"sender2";
    data71.fileName = @"文件名";
    data71.length = 1239;
    data71.sender = @"221";

    HHFileMessageCellData *data72 = [[HHFileMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data72.conversationId = self.conversationId;
    data72.time = @"1622613104353";
    data72.name = @"sender2";
    data72.fileName = @"文件名1";
    data72.length = 1239;
    data72.flagName = @"微信";
    data72.flagImage = [UIImage imageNamed:@"wechat"];
    
    
    HHFileMessageCellData *data73 = [[HHFileMessageCellData alloc] initWithDirection:HHMsgDirectionIncoming];
    data73.conversationId = self.conversationId;
    data73.time = @"1622613104353";
    data73.name = @"sender2";
    data73.fileName = @"文件名1";
    data73.length = 1239;
    data73.flagName = @"微信";
//    data73.flagImage = [UIImage imageNamed:@"wechat"];
    
    NSArray *arr = @[data, data1, data2, data3,
                     data11,
                     data9, data91,
                     data12,
                     data31, data32,
                     data41, data42,
                     data51,
                     data61,
                     data71, data72, data73];
    
    if (complation) {
        complation(YES, arr);
    }
    
}

- (void)sendMessageWithComplation:(void (^)(BOOL, NSArray * _Nonnull))complation {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (complation) {
            complation(YES, @[]);
        }
    });
    
}

- (void)sendMessage:(HHMessageCellData *)msg complation:(void (^)(BOOL, HHMessageCellData * _Nonnull))complation {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (complation) {
            complation(YES, msg);
        }
    });
}

#pragma mark - HHMessagesViewControllerDelegate

- (void)messageController:(HHMessagesViewController *)controller onSelectMoreItem:(HHKeyboardMoreItem *)item {
    NSLog(@"%@", item.title);
    
        
    switch (item.type) {
        case HHKeyboardMoreItemTypePhoto:
        {
//            [HHPhotoTool imagePickerMultipleWithController:self count:9 seletedVideo:NO completion:^(NSArray<HHPhotoModel *> * _Nonnull images) {
//
//                for (HHPhotoModel *model in images) {
//                    [self sendMessageImage:model.image];
//                }
//            }];
            
            
            [self.noticeCallContentView relaodData:@[@"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3", @"1", @"2", @"3"]];
            
            [self.noticeView show:self.view title:@"开始" type:HHNoticeTypeOpen contentView:self.noticeCallContentView];

        }
            break;
        case HHKeyboardMoreItemTypeCamera:
        {
            [self.noticeView dismiss];
        }
            break;
            
        default:
            break;
    }
    
}

- (void)messageController:(HHMessagesViewController *)controller onSelectMessageAvatar:(HHMessageCell *)cell {
    NSLog(@"头像");
}

- (void)messageController:(HHMessagesViewController *)controller onSelectMessageContent:(HHMessageCell *)cell {
    NSLog(@"111111");
}

- (BOOL)messageController:(HHMessagesViewController *)controller willShowMenuInCell:(HHMessageCell *)view {
    return YES;
}

- (NSArray<NSString *> *)messageController:(HHMessagesViewController *)controller menuItemIncell:(HHMessageCell *)cell {
    NSMutableArray *arr = [NSMutableArray array];
    // 撤回 自己的消息5分钟 或者管理员3天
    NSInteger interval = [[NSDate timestamp:[NSDate date]] longLongValue] - [cell.messageData.time longLongValue];
    if (cell.messageData.direction == HHMsgDirectionOutgoing && cell.messageData.status == HHMsgStatusSucc) {
        // 毫秒
        if (interval < 60 * 5 * 1000) {
            [arr addObject:@"撤回"];
        }
    }
    
    [arr addObject:@"删除"];
    return arr;
}

- (void)messageController:(HHMessagesViewController *)controller onSelectMenu:(NSString *)title msg:(HHMessageCellData *)msg {
    if ([title isEqualToString:@"删除"]) {
        [self onDelete];
    } else {
        [self onRevoke:nil];
    }
}

- (void)messageController:(HHMessagesViewController *)controller noticeView:(HHNoticeView *)noticeView clickTitle:(NSString *)title {
//    [noticeView dismiss];
}

#pragma mark - HHNoticeCallContentDelegate

- (void)noticeCallForJoin:(HHNoticeCallContentView *)view {
    self.noticeView.open = NO;
}

- (void)noticeCallForCancel:(HHNoticeCallContentView *)view {
    self.noticeView.open = NO;
}

#pragma mark - Getter

- (HHNoticeCallContentView *)noticeCallContentView {
    if (!_noticeCallContentView) {
        _noticeCallContentView = [[HHNoticeCallContentView alloc] init];
        _noticeCallContentView.delegate = self;
    }
    return _noticeCallContentView;
}

@end
